package ace.module.oss.core.impl.controller.rest.admin.ext;

import ace.cmp.core.model.R;
import ace.cmp.security.oauth2.resource.server.core.annotation.PreAuthorizeIsUserAuthenticated;
import ace.module.oss.core.impl.service.OssService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * template author: caspar
 * template created at: 2023-12-18
 * template description: admin controller
 * generated at: 2024年1月8日 下午3:53:01
 * description: oss 上传 admin controller
 */
@Tag(name = "Oss-上传-API")
@PreAuthorizeIsUserAuthenticated
@AllArgsConstructor
@RestController
@RequestMapping(OssUploadRestAdminController.PATH)
@Valid
public class OssUploadRestAdminController {
  public static final String PATH = "/oss/admin/upload";
  public static final String PATH_UPLOAD = "/direct";

  public static final String URL_UPLOAD = PATH + PATH_UPLOAD;

  private final OssService ossService;

  @Operation(summary = "上传文件", description = "普通上传")
  @Parameters({
    @Parameter(
        description = "oss应用id",
        name = "ossAppId",
        required = true,
        in = ParameterIn.DEFAULT),
    @Parameter(description = "oss应用配置key", name = "key", required = true, in = ParameterIn.DEFAULT),
    @Parameter(description = "上传文件", name = "file", required = true, in = ParameterIn.DEFAULT),
  })
  @PostMapping(PATH_UPLOAD)
  public R<String> upload(
      @RequestParam("ossAppId") Long ossAppId,
      @RequestParam("key") String key,
      @RequestParam("file") @NotNull MultipartFile file) {
    String fileUrl = this.ossService.upload(ossAppId, key, file);
    return R.ok(fileUrl);
  }
}
